﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Modules.Clients.Services
{
    public class ClientsRepository : AutorentEntitiesContext, IClientsRepository
    {              
        public ObservableCollection<Client> GetClients()
        {
            try
            {
                return new ObservableCollection<Client>(Context.Clients);
            }
            catch (Exception)
            {
                throw new AutorentModelException("Clients loading error occured!");
            }
        }

        public void AddClient(Client client)
        {
            try
            {
                Context.AddToClients(client);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Add client error occured!");
            }
        }

        public void UpdateClient(Client client)
        {
            try
            {
                Context.UpdateObject(Context.Clients.Where<Client>(c => c.Client_ID.Equals(client.Client_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Update client error occured!");
            }
        }

        public void DeleteClient(Client client)
        {
            try
            {
                Context.DeleteObject(Context.Clients.Where<Client>(c => c.Client_ID.Equals(client.Client_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Delete client error occured!");
            }
        }
    }
}
